package cz.cizek.tutorials.designpatterns.immutableobject.wrong_non_immutable;

import java.util.HashSet;
import java.util.Set;

/**
 * @author jcizek
 */
public class Main {

    public static void main(String[] args) {
        NonImmutableFraction nonImmutableFraction1 = new NonImmutableFraction(2, 1);
        NonImmutableFraction nonImmutableFraction2 = nonImmutableFraction1;

        System.out.println("Before operation fraction 1 :" + nonImmutableFraction1.toString() +
                " fraction 2:" + nonImmutableFraction2.toString());
        nonImmutableFraction2 = nonImmutableFraction1.multiply(nonImmutableFraction2);
        System.out.println("After operation fraction 1 :" + nonImmutableFraction1.toString() +
                " fraction 2:" + nonImmutableFraction2.toString());


        //-------------------------------------------FIND IN COLLECTION-------------------------------------------------
        Set<NonImmutableFraction2> hashSet = new HashSet<NonImmutableFraction2>();
        NonImmutableFraction2 fraction1 = new NonImmutableFraction2(2, 1);
        NonImmutableFraction2 fraction2 = fraction1;
        NonImmutableFraction2 fraction3 = new NonImmutableFraction2(2, 1);

        hashSet.add(fraction1);

        checkFractionInCollection(hashSet, fraction1, fraction2, fraction3);
        fraction2 = (NonImmutableFraction2) fraction2.multiply(fraction2);
        checkFractionInCollection(hashSet, fraction1, fraction2, fraction3);
    }

    private static void checkFractionInCollection(Set<NonImmutableFraction2> hashSet, NonImmutableFraction2... fraction) {
        System.out.println("Fraction in collection: ");
        for (int i = 1; i < fraction.length; i++) {
            System.out.println("fraction" + i + ":" + hashSet.contains(fraction[i]));
        }
    }
}
